<template>
	<view class="content">
		<headerSpaceBetweenVue>
			<template #icon>
				<text @click="toggleModel()">明细</text>
			</template>
			<template #title>积分充值</template>
		</headerSpaceBetweenVue>
		<view class="recharge-page">
			<!-- 当前积分余额 -->
			<view class="balance-section">
				<text class="balance-label">当前积分余额</text>
				<text class="balance-value">{{score}}</text>
			</view>

			<!-- 积分选择 -->
			<view class="section-title">选择充值积分</view>
			<view class="option-group">
				<view class="option-item" :class="{ active: selectedPoints === item.score }" @tap="selectPoints(item)"
					v-for="item in scoreItem">
					<view class="option-selection">
						<view class="selection-circle" v-if="selectedPoints === item.score"></view>
					</view>
					<text class="option-text">{{item.score}} 积分</text>
					<text class="option-price">￥{{item.money}}</text>
				</view>

			</view>

			<!-- 支付方式 -->
			<view class="section-title">选择支付方式</view>
			<view class="option-group">
				<view class="option-item" :class="{ active: selectedPayment === 'wechat' }"
					@tap="selectPayment('wechat')">
					<view class="option-selection">
						<view class="selection-circle" v-if="selectedPayment === 'wechat'"></view>
					</view>
					<text class="option-text">微信支付</text>
				</view>
				<view class="option-item" :class="{ active: selectedPayment === 'alipay' }"
					@tap="selectPayment('alipay')">
					<view class="option-selection">
						<view class="selection-circle" v-if="selectedPayment === 'alipay'"></view>
					</view>
					<text class="option-text">支付宝</text>
				</view>
				<view class="option-item" :class="{ active: selectedPayment === 'qq' }" @tap="selectPayment('qq')">
					<view class="option-selection">
						<view class="selection-circle" v-if="selectedPayment === 'qq'"></view>
					</view>
					<text class="option-text">QQ支付</text>
				</view>

				<!-- <view class="option-item" :class="{ active: selectedPayment === 'other' }"
					@tap="selectPayment('other')">
					<view class="option-selection">
						<view class="selection-circle" v-if="selectedPayment === 'other'"></view>
					</view>
					<text class="option-text">其他支付方式</text>
				</view> -->
			</view>

			<!-- 确认按钮 -->
			<view class="confirm-btn" @tap="handleRecharge">
				确认充值
			</view>
		</view>
		<view class="useLogModel" v-if="modelShow">
			<view class="logList">
				<view class="closeModel">
					<uni-icons type="closeempty" @click="closeModel()" size="30"></uni-icons>
				</view>
				<scoreLogItemVue v-for="(item,index) in list" :item="item" :key="index"></scoreLogItemVue>
				<uni-load-more :status="hasMore && isLoading ? 'loading' : 'noMore'"></uni-load-more>
			</view>
		</view>
	</view>
</template>

<script setup>
	import headerSpaceBetweenVue from '@/components/header-space-between/header-space-between.vue';
	import scoreLogItemVue from '@/components/shop/scoreLog-item.vue';
	import {
		ref
	} from 'vue';
	import {
		onLoad,
		onReachBottom
	} from "@dcloudio/uni-app"
	import {
		apiGetScoreItem,
		apiInitPay,
		apiScoreLog
	} from '@/api/apis';
	let scoreItem = ref([])
	let score = ref(0)
	let modelShow = ref(false)
	let selectedPoints = ref(0)
	let selectedPayment = ref('wechat')
	let checkScoreItem = ref(0)
	let list = ref([])
	let page = ref(1)
	let hasMore = ref(true)
	let isLoading = ref(false)

	const getScoreLog = async (query) => {
		await apiScoreLog(query).then(res => {
			list.value = res.data.data
		})
	}
	const getScoreItem = async (query) => {
		await apiGetScoreItem(query).then(res => {
			scoreItem.value = res.data.list
			score.value = res.data.user.score
			selectedPoints.value = res.data.list[0]['score']
			checkScoreItem.value = res.data.list[0]['id']
		})
	}
	const selectPoints = (item) => {
		selectedPoints.value = item.score
		checkScoreItem.value = item.id
	}
	const selectPayment = (payment) => {
		selectedPayment.value = payment
	}
	// 处理订单
	const handleRecharge = async () => {
		uni.showLoading({
			title: '订单初始化...'
		});
		await apiInitPay({
			id: checkScoreItem.value,
			type: selectedPayment.value
		}).then(res => {
			if (res.code == 200) {
				uni.navigateTo({
					url: "/pages/pay/pay?url=" + encodeURIComponent(res.data.payLink)
				})
			}
		})

	}
	const toggleModel = () => {
		modelShow.value = !modelShow.value
	}
	const closeModel = () => {
		modelShow.value = false
	}
	getScoreItem()
	getScoreLog()
</script>

<style lang="scss" scoped>
	.useLogModel {
		position: absolute;
		left: 0;
		right: 0;
		bottom: 0;
		top: calc(var(--status-bar-height) + 90rpx);
		height: calc(100vh - var(--status-bar-height) - 90rpx);
		background-color: rgba(0, 0, 0, 0.3);
		display: flex;
		justify-content: center;
		align-items: center;

		.logList {
			width: 80%;
			background-color: #ffffff;
			height: 80%;
			overflow: auto;
			position: relative;
			.closeModel {
				display: flex;
				padding: 10rpx;
				justify-content: flex-end;
				position: sticky;
				top: 0;
				left: 0;
				right: 0;
				background-color: #fff;
			}
		}
	}

	.content {
		position: relative;
		padding: 20rpx;
		padding-top: calc(var(--status-bar-height) + 100rpx);

		.recharge-page {
			background-color: #fff;
			padding: 20px;
			font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', sans-serif;
		}

		.balance-section {
			text-align: center;
			padding: 32rpx 0;
			border-bottom: 1px solid #eee;

			.balance-label {
				display: block;
				font-size: 28rpx;
				color: #999;
				margin-bottom: 10rpx;
			}

			.balance-value {
				display: block;
				font-size: 56rpx;
				font-weight: bold;
				color: #333;
			}
		}

		.section-title {
			font-size: 30rpx;
			color: #333;
			padding: 40rpx 0 20rpx 0;
		}

		.option-group {
			border-radius: 12rpx;
			overflow: hidden;
			box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.04);
		}

		.option-item {
			display: flex;
			align-items: center;
			padding: 24rpx 32rpx;
			background-color: #fff;
			border-bottom: 1px solid #f5f5f7;

			&.active {
				background-color: #f9f9ff;
			}

			&:last-child {
				border-bottom: none;
			}
		}

		.option-selection {
			width: 40rpx;
			height: 40rpx;
			border-radius: 50%;
			border: 2rpx solid #ccc;
			margin-right: 24rpx;
			display: flex;
			justify-content: center;
			align-items: center;

			.selection-circle {
				width: 24rpx;
				height: 24rpx;
				border-radius: 50%;
				background-color: #2979ff;
			}
		}

		.option-text {
			flex: 1;
			font-size: 32rpx;
			color: #333;
		}

		.option-price {
			font-size: 32rpx;
			color: #f56c6c;
			font-weight: bold;
		}

		.confirm-btn {
			position: fixed;
			bottom: 50rpx;
			left: 5%;
			width: 90%;
			height: 88rpx;
			background-color: #2979ff;
			color: white;
			font-size: 36rpx;
			border-radius: 48rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			box-shadow: 0 8rpx 20rpx rgba(41, 121, 255, 0.3);
			transition: all 0.2s;

			&:active {
				background-color: #1a6be0;
				transform: translateY(4rpx);
			}
		}

	}
</style>